﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.houseWork.Helper;

namespace CIT.WMS.inputShelf
{
    public partial class FrminShelf : Form
    {
        bool flag = false;
        bool result = false;
        string Location_Type = string.Empty;//库位类型1：普通库位；2：智能库位
        DataTable dt_location = new DataTable();
        public FrminShelf()
        {
            InitializeComponent();

            checklgs = Utils.CheckConnectLGS(out ip, out port);
        }
        bool checklgs = false;
        string port = "";
        string ip = "";
        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        BarcodeUtils obj = new BarcodeUtils();

        private void txt_kuwei_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (checkLocation())
                {
                    Utils.SetTsLab(lab_msg, "[" + txt_kuwei.Text + "]校验合法.", new List<string> { "OK" }, Color.Lime);
                    txt_reelid.SelectAll();
                    txt_reelid.Focus();
                }
                else
                {
                    txt_kuwei.SelectAll();
                }
            }
        }
        /// <summary>
        /// 校验库位
        /// </summary>
        /// <returns></returns>
        private bool checkLocation()
        {
            if (txt_kuwei.Text.Trim() == string.Empty)
            {
                Utils.SetTsLab(lab_msg, "库位不能为空", new List<string> { "Fail" }, Color.Red);
                return false;
            }
            if (txt_kuwei.Text.Trim() != "0")
            {
                string strSql = string.Format(@" select B.Location_Type,A.SerialNumber,B.MainBoardId,B.PositionId FROM dbo.T_Bllb_StorageLocation_tbsl B 
 LEFT join  dbo.T_Bllb_StockInfo_tbsi A  ON A.Location_SN=b.Location_SN where B.Location_SN='{0}'", txt_kuwei.Text.Trim());
                DataTable dt_location = Utils.NMS.QueryDataTable(Utils.uContext, strSql, new CIT.WMS.MESWcf.CmdParameter[0]);
                if (dt_location.Rows.Count > 0)
                {
                    Location_Type = SqlInput.ChangeNullToString(dt_location.Rows[0]["Location_Type"]);//库位类型
                    if (Location_Type == "2" & SqlInput.ChangeNullToString(dt_location.Rows[0]["SerialNumber"]) != string.Empty)//判断是否为智能货架(智能存放一个物料)
                    {
                        Utils.SetTsLab(lab_msg, "库位已经绑定物料", new List<string> { "Fail" }, Color.Red);
                        return false;
                    }
                }
                else
                {
                    Utils.SetTsLab(lab_msg, "库位不存在", new List<string> { "Fail" }, Color.Red);
                    return false;
                }
            }
            return true;
        }
        private void txt_reelid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                upload();
            }
        }
        void upload()
        {
            if (!checkLocation())
            {
                return;
            }
            string strSql = string.Empty;
            string strStatus = string.Empty;
            string areaSN = string.Empty;//库区
            strStatus = string.Format(@"	SELECT * FROM T_Bllb_StockInfo_tbsi WHERE SerialNumber='{0}' AND   Lock_Flag='0'", txt_reelid.Text.Trim());
            DataTable dt_existStatus = Utils.NMS.QueryDataTable(Utils.uContext, strStatus, new CIT.WMS.MESWcf.CmdParameter[0]);
            if (dt_existStatus.Rows.Count == 0)
            {
                Utils.SetTsLab(lab_msg, "ReelID状态不对，不允许上架", new List<string> { "Fail" }, Color.Red);
                txt_reelid.SelectAll();
                return;
            }
            strSql = string.Format(@" SELECT B.Storage_Name, A.Storage_SN, A.Location_SN,A.SerialNumber,A.Enable FROM  dbo.T_Bllb_StockInfo_tbsi A 
LEFT JOIN dbo.T_Bllb_Storage_tbs B ON A.Storage_SN=B.Storage_SN where A.SerialNumber='{0}'", txt_reelid.Text.Trim());
            DataTable dt_ReelId = Utils.NMS.QueryDataTable(Utils.uContext, strSql, new CIT.WMS.MESWcf.CmdParameter[0]);
            if (dt_ReelId.Rows.Count == 0)
            {
                Utils.SetTsLab(lab_msg, "物料不仓库内", new List<string> { "Fail" }, Color.Red);
                txt_reelid.SelectAll();
                return;
            }
            else
            {
                string locationSN = SqlInput.ChangeNullToString(dt_ReelId.Rows[0]["Location_SN"]);
                if (locationSN != string.Empty)
                {
                    Utils.SetTsLab(lab_msg, "物料已在库位" + locationSN + "上", new List<string> { "Fail" }, Color.Red);
                    txt_reelid.SelectAll();
                    return;
                }
                DataTable dt_storage = Common.Bll_Common.GetListOfStorage(txt_kuwei.Text.Trim());//通过库位获取仓库SN
                if (dt_storage.Rows.Count > 0)
                {
                    if (SqlInput.ChangeNullToString(dt_storage.Rows[0]["Storage_SN"]) != SqlInput.ChangeNullToString(dt_ReelId.Rows[0]["Storage_SN"]))//判断库位是否在物料入库时所选择的仓库
                    {
                        Utils.SetTsLab(lab_msg, "物料不在当前仓库，在" + SqlInput.ChangeNullToString(dt_storage.Rows[0]["Storage_Name"]) + "中", new List<string> { "Fail" }, Color.Red);
                        txt_reelid.SelectAll();
                        return;
                    }
                    areaSN = SqlInput.ChangeNullToString(dt_storage.Rows[0]["Area_SN"]);
                }
            }
            strSql = string.Format(@"UPDATE dbo.T_Bllb_StockInfo_tbsi SET Location_SN ='{1}',Area_SN='{2}' WHERE SerialNumber='{0}'", txt_reelid.Text.Trim(), txt_kuwei.Text.Trim(), areaSN);
            Utils.NMS.Exectransql(Utils.uContext, strSql, new CIT.WMS.MESWcf.CmdParameter[0], out flag, out result);
            Utils.SetTsLab(lab_msg, "上架[" + txt_kuwei.Text + "]成功.", new List<string> { "OK" }, Color.Lime);
            //智能货架上架成功亮灯
            if (Location_Type == "2" & txt_kuwei.Text.Trim() != "0")
            {
                Utils.LightOrder(new List<StorageLight>() { new StorageLight() { ContinuedTime = 10, lightOrder = 2, MainBoardId = int.Parse(dt_location.Rows[0]["MainBoardId"].ToString()), RackPositionId = dt_location.Rows[0]["PositionId"].ToString() } }, checklgs, ip, port);
            }

            //是否连续上架
            if (ckbContinuity.Checked)
            {
                DataTable dtrack = Utils.NMS.QueryDataTable(Utils.uContext, @" select TOP 1 B.Location_SN,B.MainBoardId,B.PositionId FROM dbo.T_Bllb_StorageLocation_tbsl B 
 LEFT join  dbo.T_Bllb_StockInfo_tbsi A  ON A.Location_SN=b.Location_SN WHERE A.SerialNumber IS NULL AND B.LOCATION_TYPE=2 and  B.Location_SN!='0' ORDER BY Location_SN ASC", new CIT.WMS.MESWcf.CmdParameter[0]);
                txt_kuwei.Text = dtrack.Rows[0]["Location_SN"].ToString();
                txt_reelid.Text = "";
                txt_reelid.SelectAll();
                txt_reelid.Focus();
                return;
            }

            txt_kuwei.Text = "";
            txt_reelid.Text = "";
            txt_kuwei.SelectAll();
            txt_kuwei.Focus();

            //是否检查下一个库位

            return;
            //}
            //}
        }

        private void btn_yes_Click(object sender, EventArgs e)
        {
            upload();
        }
    }
}